package com.suxin.thread.manager.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;

/**
 * @author Tang
 * @classname ExecutorFactory
 * @description [  ]
 * @date 2022/8/11 16:22
 */
public class ExecutorFactory {

    private static final ThreadPoolManager THREAD_POOL_MANAGER = ThreadPoolManager.getInstance();

    private static final String NAMESPACE = "application";

    public static class Managed{

        public static ScheduledExecutorService newScheduledExecutorService(String groupName,
                                                                           int threadCount,
                                                                           ThreadFactory threadFactory) {
            ScheduledExecutorService executorService = Executors.newScheduledThreadPool(threadCount, threadFactory);
            THREAD_POOL_MANAGER.register(NAMESPACE,groupName,executorService);
            return executorService;
        }

        public static ExecutorService newSingleThreadExecutor(String groupName,ThreadFactory threadFactory) {
            ExecutorService executorService = Executors.newSingleThreadExecutor(threadFactory);
            THREAD_POOL_MANAGER.register(NAMESPACE,groupName,executorService);
            return executorService;
        }

        public static ExecutorService newFixedThreadPool(String groupName,
                                                         int threadCount,
                                                         ThreadFactory threadFactory) {
            ExecutorService executorService = Executors.newFixedThreadPool(threadCount,threadFactory);
            THREAD_POOL_MANAGER.register(NAMESPACE,groupName,executorService);
            return executorService;
        }

    }

}
